package com.mengchengnet.chaperonage;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * Created by P.Liu on 15-12-24.
 * Activity的基类
 */
public class BaseActivity extends AppCompatActivity {

    public static BaseActivity currentActivity; //当前Activity

    private static final int BASE_VIEW_ID = R.layout.activity_base;
    private static final LinearLayout.LayoutParams LAYOUT_PARAMS = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
    private LinearLayout mParentView;

    private Menu menu;
    private TextView tvToolbarTitle;
    private LinearLayout llCity;
    private TextView tvToolbarPosition;
    private ImageView iv_icon;

    /**
     * 标题栏状态
     */
    public static int toolbarState;

    public static final int TOOLBAR_STYLE_NONE = 0;
    public static final int TOOLBAR_STYLE_BACK_TITLE = 1;
    public static final int TOOLBAR_STYLE_HOME = 2;
    public static final int TOOLBAR_STYLE_TITLE = 3;
    public static final int TOOLBAR_STYLE_TIME = 4;
    public static final int TOOLBAR_STYLE_ADDADDRESS = 5;
    public static final int TOOLBAR_ONLY_TITLE=6;
    public static final int TOOLBAR_SHOW_TEXT=7;

    /*
     * *******************************************
     *
     * 生命周期 开始
     *
     * *********************************************
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        //记录当前活动
        currentActivity = this;
        setContentView(R.layout.activity_base);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        // 隐藏键盘
//        this.getWindow().setSoftInputMode(
//                WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    }

    /**
     * 初始化contentview
     */
    @Override
    public void setContentView(int layoutResID) {
        if (BASE_VIEW_ID == layoutResID) {
            super.setContentView(layoutResID);

            mParentView = (LinearLayout) findViewById(R.id.activity_parent);
            return;
        }

        mParentView.addView(getLayoutInflater().inflate(layoutResID, null), LAYOUT_PARAMS);
        forceShowOverflowMenu();
        tvToolbarTitle = (TextView) findViewById(R.id.tvToolbarTitle);
        llCity = (LinearLayout) findViewById(R.id.ll_city);
        tvToolbarPosition = (TextView) findViewById(R.id.tvToolbarPosition);
        iv_icon= (ImageView) findViewById(R.id.toolbar_iv_icon);
    }

    public void setChooseCityListener(View.OnClickListener onClickListener) {
        llCity.setOnClickListener(onClickListener);
    }

    public void setCity(String cityName) {
        tvToolbarPosition.setText(cityName);
    }

    @Override
    public void setContentView(View view) {
        mParentView.addView(view, LAYOUT_PARAMS);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * 如果设备有物理菜单按键，需要将其屏蔽才能显示OverflowMenu
     */
    private void forceShowOverflowMenu() {
        try {
            ViewConfiguration config = ViewConfiguration.get(this);
            Field menuKeyField = ViewConfiguration.class
                    .getDeclaredField("sHasPermanentMenuKey");
            if (menuKeyField != null) {
                menuKeyField.setAccessible(true);
                menuKeyField.setBoolean(config, false);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // OverflowShow显示图标
    @Override
    public boolean onMenuOpened(int featureId, Menu menu) {
        if (menu != null) {
            if (menu.getClass().getSimpleName().equals("MenuBuilder")) {
                try {
                    Method method = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
                    method.setAccessible(true);
                    method.invoke(menu, true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return super.onMenuOpened(featureId, menu);
    }


    /***
     * 隐藏Toolbar的Menu
     */
    private void hideMenu() {
        if (null != menu) {
            for (int i = 0; i < menu.size(); i++) {
                menu.getItem(i).setVisible(false);
            }
        }
    }

    /***
     * 显示Toolbar的Menu
     */
    private void showMenu() {
        if (null != menu) {
            for (int i = 0; i < menu.size(); i++) {
                menu.getItem(i).setVisible(true);
            }
        }
    }

    /**
     * 退出按键的设计
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if (keyCode == KeyEvent.KEYCODE_BACK) {
//            if (currentActivity instanceof MainTabActivity || currentActivity instanceof WelcomeActivity) {
//
//                ViewUtil.alert("确定要退出河北一卡通吗？", new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//                        killMobclickAgent();
//                        finish();
//                        //android.os.Process.killProcess(android.os.Process.myPid());// 退出线程
//                    }
//                });
//
//                return false;
//            }
//        }

        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onResume() {
        super.onResume();
        currentActivity = this;
    }

    @Override
    protected void onPause() {
        super.onPause();
        currentActivity = this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public void initToolbar(String title) {

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("");
        toolbar.setNavigationIcon(null);
        setSupportActionBar(toolbar);

        switch (toolbarState) {
            case TOOLBAR_STYLE_NONE:
                if (getSupportActionBar().isShowing()) {
                    getSupportActionBar().hide();
                }
                break;
            case TOOLBAR_STYLE_BACK_TITLE:
                //隐藏菜单栏
                hideMenu();
                //隐藏自带标题
                toolbar.setTitle("");
                //设置自定义标题
                tvToolbarTitle.setText(title);
                llCity.setVisibility(View.INVISIBLE);
                //设置返回按钮
                getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                //显示标题栏
                getSupportActionBar().show();
                break;
            case TOOLBAR_STYLE_HOME:
                //显示菜单栏
                hideMenu();
                //隐藏自带标题
                toolbar.setTitle("");
                //设置自定义标题
                tvToolbarTitle.setText(title);
                llCity.setVisibility(View.VISIBLE);
                //设置图标
                // /toolbar.setNavigationIcon(R.mipmap.ic_login);
                //设置返回按钮
                getSupportActionBar().setDisplayHomeAsUpEnabled(false);
                //显示标题栏
                getSupportActionBar().show();
                break;
            case TOOLBAR_STYLE_TITLE:
                //显示菜单栏
                hideMenu();
                //设置自带标题
                toolbar.setTitle("");
                //隐藏自定义标题
                tvToolbarTitle.setText(title);
                llCity.setVisibility(View.INVISIBLE);
                //设置返回按钮
                getSupportActionBar().setDisplayHomeAsUpEnabled(false);
                //显示标题栏
                getSupportActionBar().show();
                break;
            case TOOLBAR_STYLE_TIME:
                //显示菜单栏
                hideMenu();
                //隐藏自带标题
                toolbar.setTitle("");
                //设置自定义标题
                tvToolbarTitle.setText(title);
                llCity.setVisibility(View.INVISIBLE);
                //设置图标
                toolbar.setNavigationIcon(R.mipmap.ic_login);
                //设置返回按钮
                getSupportActionBar().setDisplayHomeAsUpEnabled(false);
                //显示标题栏
                getSupportActionBar().show();
                break;

            case TOOLBAR_STYLE_ADDADDRESS:
                //隐藏菜单栏
                hideMenu();
                //隐藏自带标题
                toolbar.setTitle("");
                //设置自定义标题
                tvToolbarTitle.setText(title);
                llCity.setVisibility(View.GONE);
                //设置返回按钮
                getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                iv_icon.setVisibility(View.VISIBLE);
                //显示标题栏
                getSupportActionBar().show();
                break;

            case TOOLBAR_ONLY_TITLE:
                //隐藏菜单栏
                hideMenu();
                //隐藏自带标题
                toolbar.setTitle("");
                //设置自定义标题
                tvToolbarTitle.setText(title);
                llCity.setVisibility(View.GONE);
                //设置返回按钮
                getSupportActionBar().setDisplayHomeAsUpEnabled(false);
                iv_icon.setVisibility(View.INVISIBLE);
                //显示标题栏
                getSupportActionBar().show();
                break;
            case TOOLBAR_SHOW_TEXT:
                //隐藏菜单栏
                hideMenu();
                //隐藏自带标题
                toolbar.setTitle("");
                //设置自定义标题
                tvToolbarTitle.setText(title);
                llCity.setVisibility(View.VISIBLE);
                //设置返回按钮
                getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                iv_icon.setVisibility(View.GONE);
                //显示标题栏
                getSupportActionBar().show();
                break;
        }

    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
    }

//    public void startActivity(Class<?> c) {
//        Intent intent = new Intent(this, c);
//        startActivity(intent);
//        overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
//    }

    public void startActivityAndFinish(Class<?> c) {
        Intent intent = new Intent(this, c);
        startActivity(intent);
        finish();
        overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.phone_in, R.anim.slide_to_right);
    }

}
